package net.zjitc.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.zjitc.common.ResultMessage;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProductService;
import net.zjitc.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Author admin
 * @DATE 2019/3/29 12:29
 */
@Controller
@RequestMapping("/manage/product")
public class AdminProductController {

    @Autowired
    ProductService productService;
    @Autowired
    CategoryService categoryService;

    //查询所有商品
    @RequestMapping("/findAllProduct")
    public String findAll(Model model){
        List<Product> list = productService.findAll();
        List<Category> categoryList = categoryService.findAll();
        model.addAttribute("list",list);
        model.addAttribute("clist",categoryList);
        return "/admin/products/list.jsp";
    }

    //修改
    @RequestMapping("/editUI")
    public String editUI(String id,Model model){
        Product product = productService.findById(id);
        List<Category> categoryList = categoryService.findAll();
        model.addAttribute("clist",categoryList);
        model.addAttribute("p",product);
        return "/admin/products/edit.jsp";
    }

    //修改产品
    @RequestMapping("/editProduct")
    @ResponseBody
    public ResultMessage editProduct(Product product,
                                     @RequestParam(value = "file",required = false) MultipartFile file,
                                     HttpServletRequest request){
        if (file!= null){
            String path = request.getServletContext().getRealPath("/products/6/");
            if (!processFile(product,file,path)){
                return ResultMessage.createSuccessMessage("商品修改失败");
            }
        }
        int resultCount = productService.editProduct(product);
        if (resultCount>0){
            return ResultMessage.createSuccessMessage("商品修改成功");
        }
        return ResultMessage.createErrorMessage("商品修改失败");

    }

    //提示商品添加成功
    @RequestMapping("/addProduct")
    @ResponseBody
    public ResultMessage addProduct(Product product,
                                    @RequestParam(value = "file",required = false) MultipartFile file,
                                    HttpServletRequest request){
        if (file!= null){
            String path = request.getServletContext().getRealPath("/products/6/");
           boolean flag = processFile(product,file,path);
           if (flag != true){
              return ResultMessage.createSuccessMessage("文件上传失败");
           }
        }
        int resultCount = productService.addProduct(product);
        if (resultCount>0){
            return ResultMessage.createSuccessMessage("商品添加成功");
        }
        return ResultMessage.createErrorMessage("商品添加失败");
    }

    //删除产品
    @RequestMapping("/deleteProduct")

    public String deleteProduct(String id) {
        productService.deleteProduct(id);
//        if (resultCount>0){
//            return ResultMessage.createSuccessMessage("商品删除成功",0);
        return "redirect:/manage/product/findAllProduct.do";
//        }
//        return ResultMessage.createErrorMessage("商品删除失败",1);
    }

    private boolean processFile(Product product, MultipartFile file, String path) {
        if (file != null) {
            //创建文件夹
            File dir = new File(path);
            if ((!dir.exists()) || (!dir.isDirectory())) {
                dir.mkdirs();
            }
            //在文件夹下创建文件，文件名为:uuid_文件名
            String filename = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
            File filePath = new File(dir, filename);

            try {
                //将上传的文件写入目标文件
                file.transferTo(filePath);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            product.setImgurl("/productImg/16/" + filename);
        }
        return true;
    }

    @RequestMapping("/addProductUI")
    public String addProductUI(Model model){
        List<Category> categoryList = categoryService.findAll();
        model.addAttribute("list",categoryList);
        return "/admin/products/add.jsp";

    }

    //查询
    @RequestMapping("/searchProduct")
    public String searchProduct(ProductVo vo,Model model){
        List<Product> list = productService.searchByCondition(vo);
        model.addAttribute("list",list);
        return "/admin/products/list.jsp";
    }
    @RequestMapping("/search")
    @ResponseBody
    public List<Product> search(ProductVo vo){
        List<Product> list = productService.searchByCondition(vo);
        return list;
    }
}
